package cn.gdcp.lkqapp03;

import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import java.util.HashMap;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private WebView my_WebView;
    private Button btn_loadJS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_WebView = findViewById(R.id.my_webview);

        my_WebView.getSettings().setJavaScriptEnabled(true);
        my_WebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        my_WebView.addJavascriptInterface(new JStoAndroid(MainActivity.this), "test");


        my_WebView.loadUrl("file:///android_asset/test.html");

        my_WebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("MainActivity","Url = " + url);
                Uri uri = Uri.parse(url);
                Log.e("MainActivity", "getScheme = " + uri.getScheme());
                Log.e("MainActivity", "getAuthority = " + uri.getAuthority());

                if (uri.getScheme().equals("js")){
                    if (uri.getAuthority().equals("webview")){
                        Log.e("MainActivity", "通过协议调用到了安卓代码");

                        HashMap<String,String> params = new HashMap<>();
                        Set<String> collection = uri.getQueryParameterNames();
                        Log.e("MainActivity", "collection = " + collection.toString());

                        return true;
                    }
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });


        my_WebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Js Alert")
                        .setMessage(message)
                        .setCancelable(true)
                        .create();
                alertDialog.show();
                return true;

                /*return super.onJsAlert(view, url, message, result);*/
            }
        });

        //第一种方式 通过loadUrl()
        /*btn_loadJS = findViewById(R.id.btn_loadJS);
        btn_loadJS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                my_WebView.loadUrl("javascript:callJS()");
            }
        });*/

        //第二种方式 通过evaluateJavascript
        btn_loadJS = findViewById(R.id.btn_loadJS);
        btn_loadJS.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                my_WebView.evaluateJavascript("javascript:callJS()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {

                    }
                });
            }
        });

    }
}
